package com.hycat.mail.product.service.impl;

import com.hycat.mail.common.to.es.SkuEsModel;
import com.hycat.mail.product.client.SearchClient;
import com.hycat.mail.product.client.WareClient;
import com.hycat.mail.common.exception.MailException;
import com.hycat.mail.common.model.ExceptionEnum;
import com.hycat.mail.product.entity.*;
import com.hycat.mail.product.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hycat.mail.common.utils.PageUtil;
import com.hycat.mail.common.utils.Query;
import com.hycat.mail.product.dao.SpuInfoDao;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Resource
    private WareClient wareClient;

    @Resource
    private SearchClient searchClient;

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public void up(Long spuId) {
        /*封装SkuEsModel信息 */
        SkuEsModel skuEsModel = new SkuEsModel();
        List<SkuInfoEntity> skuInfoList = skuInfoService.listBySpuId(spuId);

        //库存
        Map<Long, Long> mapStock = null;
        try {
            List<Long> skuIds = skuInfoList.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());
            mapStock = wareClient.stockDetails(skuIds);
        } catch (Exception e) {
            throw new MailException(ExceptionEnum.GET_STOCK_ERROR, e);
        }

        Map<Long, Long> finalMapStock = mapStock;
        List<SkuEsModel> skuEsModels = skuInfoList.stream()
                .map((sku) -> {
                    BeanUtils.copyProperties(sku, skuEsModel);
                    skuEsModel.setSkuPrice(sku.getPrice());
                    skuEsModel.setSkuImg(sku.getSkuDefaultImg());

                    //品牌、分类
                    CategoryEntity category = categoryService.getById(sku.getCatalogId());
                    skuEsModel.setCatelogName(category.getName());

                    BrandEntity brand = brandService.getById(sku.getBrandId());
                    skuEsModel.setBrandName(brand.getName());
                    skuEsModel.setBrandImg(brand.getLogo());

                    //处理库存
                    Long stock = finalMapStock.get(sku.getSkuId());
                    skuEsModel.setHasStock(stock != null && stock > 0);

                    //可检索的商品attr属性
                    List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.listBySpuId(sku.getSpuId());
                    List<Long> attrIds = productAttrValueEntities.stream().map(ProductAttrValueEntity::getAttrId).collect(Collectors.toList());

                    List<AttrEntity> attrs = attrService.listSearchByIds(attrIds);
                    attrIds = attrs.stream().map(AttrEntity::getAttrId).collect(Collectors.toList());
                    List<Long> finalAttrIds = attrIds;

                    List<SkuEsModel.Attrs> collect = productAttrValueEntities.stream().filter((pav) -> finalAttrIds.contains(pav.getAttrId())).map((pav) -> {
                        SkuEsModel.Attrs attrs1 = new SkuEsModel.Attrs();
                        BeanUtils.copyProperties(pav, attrs1);
                        return attrs1;
                    }).collect(Collectors.toList());
                    skuEsModel.setAttrs(collect);

                    //TODO 得分
                    skuEsModel.setHotScore(0L);

                    return skuEsModel;
                }).collect(Collectors.toList());

        /*远程调用 更新ES*/
        String code = (String) searchClient.productSpuUp(skuEsModels).get("code");
        if(!StringUtils.isEmpty(code) && code.equals("0")) {
            baseMapper.up(spuId);//修改Spu上下架状态
        }

    }
}